#include <iostream>
#include <fstream>
#include "bundle.h"

using namespace std;

void Usage(char* argv[])
{
    cout << "=======================================" << endl;
    cout  << "Usage:" << endl;
    cout << "\t" << argv[0] << " " << "原始文件路径" << " " << "压缩包名称" << endl;
    cout << "=======================================" << endl;
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        Usage(argv);
        return -1;
    }

    // 打开文件(读)
    ifstream ifs;
    ifs.open(argv[1], ios::binary);

    // 获取文件的大小
    size_t fsize = 0;
    ifs.seekg(0, ios::end);
    fsize = ifs.tellg();
    ifs.seekg(0, ios::beg);

    // 读取文件
    string body;
    body.resize(fsize);
    ifs.read(&body[0], fsize);

    // 创建新的压缩文件
    string packed = bundle::pack(bundle::LZIP, body);

    // 打开文件(写)
    ofstream ofs;
    ofs.open(argv[2], ios::binary);

    ofs.write(packed.c_str(), packed.size()); // 写入文件


    ifs.close();
    ofs.close();
    body.clear();

    // 解压
    body = bundle::unpack(packed);

    // 写入至新的文件中
    ofs.open("newFile.txt", ios::binary);
    ofs.write(body.c_str(), body.size());

    ifs.close();
    ofs.close();

    return 0;
}